package com.iv.durian.cloud.api;

import com.iv.durian.cloud.Banner;
import com.iv.durian.cloud.JsonResult;
import com.iv.durian.cloud.api.service.BannerService;
import com.iv.durian.cloud.vo.BannerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/banner")
@Slf4j
@Api(value = "BannerApi", description = "BannerApi", tags = {"banner"})
public class BannerApi {
    @Autowired
    private BannerService bannerService;

    @ApiOperation(value = "横幅列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JsonResult<List<BannerVO>> list(
            @ApiParam(required = true, name = "type", value = "类型") @RequestParam("type") Integer type,
            @ApiParam(required = true, name = "status", value = "状态") @RequestParam("status") Integer status
    ) {
        JsonResult r = new JsonResult();
        List<Banner> bannerList = bannerService.list(type,status);
        List<BannerVO> bannerVOList =  bannerList.stream().map(banner -> {
            BannerVO bannerVO = BannerVO.builder()
                    .id(banner.getId())
                    .image(banner.getImage())
                    .url(banner.getUrl())
                    .type(banner.getType().ordinal())
                    .createTime(banner.getCreateTime())
                    .updateTime(banner.getUpdateTime())
                    .build();
            return bannerVO;
        }).collect(Collectors.toList());
        r.ok(bannerVOList);
        return r;
    }

    @ApiOperation(value = "获取横幅信息")
    @RequestMapping(value = "/get/{banner_id}", method = RequestMethod.GET)
    public JsonResult<BannerVO> get(
            @ApiParam(required = true, name = "banner_id", value = "横幅id") @PathVariable("banner_id") Integer bannerId) {
        JsonResult r = new JsonResult();
        Banner banner = bannerService.get(bannerId);
        Assert.notNull(banner,"横幅不存在");
        BannerVO bannerVO = BannerVO.builder()
                .id(banner.getId())
                .image(banner.getImage())
                .url(banner.getUrl())
                .type(banner.getType().ordinal())
                .build();
        r.ok(bannerVO);
        return r;
    }

    @ApiOperation(value = "修改横幅信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void update(
            @ApiParam(required = true, name = "banner", value = "banner对象") @RequestBody Banner banner) {
        bannerService.update(banner);
    }

    @ApiOperation(value = "新增横幅信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void add(
            @ApiParam(required = true, name = "image", value = "封面") @RequestParam String image,
            @ApiParam(required = true, name = "url", value = "跳转链接") @RequestParam String url
            ) {
        Banner banner = new Banner();
        banner.setImage(image);
        banner.setUrl(url);
        bannerService.add(banner);
    }

    @ApiOperation(value = "删除横幅")
    @RequestMapping(value = "/remove/{banner_id}", method = RequestMethod.GET)
    public void remove(
            @ApiParam(required = true, name = "banner_id", value = "横幅id") @PathVariable("banner_id") Integer bannerId) {
        bannerService.remove(bannerId);
    }

    @ApiOperation(value = "计算横幅的数量")
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    public int count(
            @ApiParam(required = true, name = "type", value = "类型") @RequestParam("type") Integer type,
            @ApiParam(required = true, name = "status", value = "状态") @RequestParam("status") Integer status
    ) {
        return bannerService.count(type,status);
    }

}
